package com.yyk.yykywd.mvvm.ui

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.mvvm.adapter.CustomerAdapter
import com.yyk.yykywd.mvvm.entity.MeetingDetailBean
import com.yyk.yykywd.mvvm.view_model.MeetingInvitationViewModel
import com.yyk.yykywd.util.ToAnotherActivity
import kotlinx.android.synthetic.main.activity_meeting_invitation_detail.*
import kotlinx.android.synthetic.main.layout_load.*
import kotlinx.android.synthetic.main.rl_bottom.*

class MeetingInvitationDetailActivity : BaseActivity() {
    private lateinit var viewModel: MeetingInvitationViewModel
    private lateinit var customerAdapter: CustomerAdapter


    override fun initView() {
        viewModel = ViewModelProviders.of(this).get(MeetingInvitationViewModel::class.java)
        viewModel.detailId = intent.getStringExtra(ID)

        customerAdapter = CustomerAdapter(mActivity, R.layout.item_invited_customer, viewModel.mCustomers)
        with(rv) {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = customerAdapter
        }

        setBackArrow()
        setTitle("会议邀约详情")
        tv_execute.text = "修改会议"
    }

    override fun setListener() {
        cl_expand.setOnClickListener {
            if (getTextString(tv_expand) == getString(R.string.expand_all)) {
                tv_expand.text = getString(R.string.pack_up)
                viewModel.mCustomers.clear()
                viewModel.mCustomers.addAll(viewModel.mCustomerAll)
                customerAdapter.notifyDataSetChanged()
                tv_expand.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_sq, 0)
            } else {
                viewModel.mCustomers.clear()
                viewModel.mCustomers.addAll(viewModel.mCustomerParts)
                customerAdapter.notifyDataSetChanged()
                tv_expand.text = getString(R.string.expand_all)
                tv_expand.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_zkkqb, 0)
            }
        }

        viewModel.mMeetingDetail.observe(this, Observer<MeetingDetailBean> {
            with(it.data) {
                tv_founder.text = "会议创建人：" + uname
                tv_create_time.text = "创建时间：" + getCommonTime(create_time)
                tv_meeting_name.text = name
                tv_meeting_region.text = region
                tv_meeting_site.text = site
                tv_meeting_start_time.text = getCommonTime(start_time)
                tv_meeting_overview.text = remark
                tv_return_money_num.text = money
                tv_attendance.text = "${num}人"
                tv_meeting_summary.text = summary

                customerAdapter.notifyDataSetChanged()
                if (viewModel.mCustomerParts.size < 6) {
                    cl_expand.visibility = View.GONE
                } else {
                    cl_expand.visibility = View.VISIBLE
                }

                viewModel.detailStatus = status
                when (status) {
                    1 -> {
                        setLayout()
                        tv_execute.text = "修改会议"
                        iv.setImageResource(R.drawable.icon_hywks)
                    }
                    2 -> {
                        setLayout()
                        tv_execute.text = "会议结束"
                        iv.setImageResource(R.drawable.icon_hyyks)
                    }
                    3 -> {
                        setLayout()
                        rl.visibility = View.GONE
                        cl_meeting_summary.visibility = View.VISIBLE
                        iv.setImageResource(R.drawable.icon_hyyjs)
                    }
                }

                loading_layout.showContent()
                nsv.visibility = View.VISIBLE
            }
        })

        tv_execute.setOnClickListener {
            if (whetherHaveMeetingPermission()) {
                return@setOnClickListener
            }
            when (viewModel.detailStatus) {
                1 -> {
                    ToAnotherActivity.toCreateOrModifyMeetingInvitationActivity(mActivity, MODIFY, viewModel.detailId)
                }
                2 -> {
                    ToAnotherActivity.toMeetingEndActivity(mActivity, viewModel.detailId)
                }
            }
        }
    }

    private fun setLayout() {
        if (viewModel.mCustomerParts.size == 0) {
            cl_customer.visibility = View.GONE
        } else {
            cl_customer.visibility = View.VISIBLE
        }
        cl_meeting_summary.visibility = View.GONE
    }

    override fun initData() {
        loading_layout.showLoading()
    }

    override fun onResume() {
        super.onResume()
        viewModel.getMeetingDetail()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_meeting_invitation_detail
    }

}
